/*
 * SearchFrame.java
 *
 * Created on 5 September 2006, 8:22
 */
package fasea.ui.search;

import fasea.search.SearchResults;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JRootPane;
import javax.swing.JScrollBar;
import javax.swing.KeyStroke;

/**
 *
 * @author gvasil
 */
public class SearchViewImpl extends javax.swing.JFrame implements SearchView {

    /**
     * Creates new form SearchFrame
     */
    public SearchViewImpl() {
        this.setUndecorated(true);
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

        //add ESC-Listener
        ActionListener actionListener = new ActionListener() {

            public void actionPerformed(ActionEvent actionEvent) {
                setVisible(false);
            }
        };
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        this.getRootPane().registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        /*
         * initialize tray icon
         */

        try {
            this.trayIconControl = new TrayIconControl(this);
            this.trayIconControl.add();
            this.trayIconControl.setToolTip(
                    "Fasea Search");
        } catch (UnsupportedOperationException ex) {
            this.trayIconControl = null;
            System.err.println(ex.getMessage());
        }


        initComponents();
        //center dialog
        this.setLocationRelativeTo(null);
        //this.init();

        /*
         * idiot patch to initialize classic panel
         */
        this.resultsPanel = new ClassicResultsPanel();

        this.scrollPane.setViewportView(
                this.resultsPanel);
    }

    public void setIndexAlertIcon() {
        lastIndexedLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alert-important.png")));
        lastIndexedLabel.setToolTipText("Index is outdated or incomplete. Please start indexer.");
    }

    public void setIndexOKIcon() {
        lastIndexedLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alert-green.png")));
        lastIndexedLabel.setToolTipText("Indexing has been completed successfully.");
    }

    public void setIndexRunningIcon() {
        lastIndexedLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/loader.gif")));
        lastIndexedLabel.setToolTipText("Indexing in progress...");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        viewButtonGroup = new javax.swing.ButtonGroup();
        scrollPane = new javax.swing.JScrollPane();
        resultsPanel = new fasea.ui.search.ResultsPanel();
        searchPanel = new javax.swing.JPanel();
        searchField = new javax.swing.JTextField();
        propertiesButton = new javax.swing.JButton();
        lastIndexedLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle(fasea.constants.Constants.getApplicationName() + " Desktop "
            + "Search v"
            + fasea.constants.Constants.getApplicationVersion());
        setBackground(new java.awt.Color(241, 240, 240));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImage(Toolkit.getDefaultToolkit()
            .getImage(getClass().
                getResource("/puggle.png")));
        setName("frame"); // NOI18N

        scrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        org.jdesktop.layout.GroupLayout resultsPanelLayout = new org.jdesktop.layout.GroupLayout(resultsPanel);
        resultsPanel.setLayout(resultsPanelLayout);
        resultsPanelLayout.setHorizontalGroup(
            resultsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 1264, Short.MAX_VALUE)
        );
        resultsPanelLayout.setVerticalGroup(
            resultsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 560, Short.MAX_VALUE)
        );

        scrollPane.setViewportView(resultsPanel);

        searchPanel.setBackground(new java.awt.Color(234, 233, 234));
        searchPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        searchField.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N

        propertiesButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/preferences.png"))); // NOI18N
        propertiesButton.setToolTipText("Set indexing properties");
        propertiesButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        propertiesButton.setBorderPainted(false);
        propertiesButton.setContentAreaFilled(false);
        propertiesButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        lastIndexedLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lastIndexedLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/alert-important.png"))); // NOI18N
        lastIndexedLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        org.jdesktop.layout.GroupLayout searchPanelLayout = new org.jdesktop.layout.GroupLayout(searchPanel);
        searchPanel.setLayout(searchPanelLayout);
        searchPanelLayout.setHorizontalGroup(
            searchPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(searchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(searchField)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(searchPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(propertiesButton)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, lastIndexedLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        searchPanelLayout.setVerticalGroup(
            searchPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(searchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(searchPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(searchPanelLayout.createSequentialGroup()
                        .add(propertiesButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(lastIndexedLabel))
                    .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)
            .add(searchPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(searchPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(4, 4, 4)
                .add(scrollPane))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void addPropertiesButtonActionListener(java.awt.event.ActionListener listener){
        propertiesButton.addActionListener(listener);
    }
    
    public void addSearchFieldKeyListener(java.awt.event.KeyAdapter listener){
        searchField.addKeyListener(listener);
    }

    private TrayIconControl trayIconControl;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lastIndexedLabel;
    private javax.swing.JButton propertiesButton;
    private fasea.ui.search.ResultsPanel resultsPanel;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextField searchField;
    private javax.swing.JPanel searchPanel;
    private javax.swing.ButtonGroup viewButtonGroup;
    // End of variables declaration//GEN-END:variables

    
    public void close() {
        this.dispose();
        System.exit(0);
    }

    public void printResults(SearchResults results) {
        resultsPanel.printResults(results);
        JScrollBar bar = scrollPane.getVerticalScrollBar();
        bar.setValue(bar.getMinimum());
    }

    public Frame getFrame() {
        return this;
    }

    public String getSearchText() {
        return searchField.getText();
    }

    public void selectResult(int index) {
        resultsPanel.markPanel(index);
    }

    public int getSelectedResultIndex() {
        return resultsPanel.getSelectedPanelIndex();
    }

    public void selectNextEntry() {
        resultsPanel.selectNextPanel();
    }

    public void selectPreviousEntry() {
        resultsPanel.selectPreviousPanel();
    }
}
